package com.chenyue.cm.api.user;

import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.user.domain.JdVerify;
import com.chenyue.cm.user.service.JdVerifyService;
import com.chenyue.cm.utils.DateUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author libingxing
 */
@ApiSort(3)
@Api(value = "实名认证/三方认证", tags = "实名认证/三方认证")
@RestController
@RequestMapping("/api/verify")
public class ApiVerifyController {

    private final JdVerifyService verifyService;

    public ApiVerifyController(JdVerifyService verifyService) {
        this.verifyService = verifyService;
    }

    @Login
    @PostMapping("/saveVerify")
    @ApiOperation("保存实名信息")
    @ApiOperationSupport(order = 1)
    public Success<Object> saveVerify(
            @ApiIgnore @LoginUser JdAppUser user,
            @ApiParam @RequestBody JdVerify verify
    ) {
        if (verify.getId() != null && verify.getId() > 0) {
            return Success.success();
        } else {
            String msg = verifyService.isBindCard(verify.getCardNumber());
            if (StringUtils.isNotBlank(msg)) {
                return Success.error(msg);
            }
            JdVerify verifyOld = verifyService.getVerifyInfo(Integer.valueOf(user.getId()));
            if (verifyOld != null) {
                return Success.error("您已经完成实名认证");
            }
            verify.setUserId(Integer.valueOf(user.getId()));
            verify.setCt(DateUtils.getToday());
            verifyService.saveVerify(verify);
        }
        return Success.success();
    }

}
